// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using Mono.Unix;
using NLog;

namespace wxGadu
{
	public class GaduTaskBarIcon : wx.TaskBarIcon
	{
		private enum Id
		{
			exitMenu,
			onlineMenu,
			awayMenu,
			invisibleMenu,
			offlineMenu,
			settingsMenu
		}
		private wx.Menu contexMenu;
		private MainFrame mainFrame;
		private ImageManager imageManager;
#if LOG
		private Logger logger = LogManager.GetCurrentClassLogger();
#endif
		
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="mainFrame">
		/// Reference to main application window
		/// A <see cref="MainFrame"/>
		/// </param>
		public GaduTaskBarIcon(MainFrame mainFrame) 
		{
			this.mainFrame = mainFrame;
			wx.Icon defaultIcon = new wx.Icon();
			defaultIcon.CopyFromBitmap(mainFrame.ImageMgr.StatusBitmaps[StatusConstans.offline]);
			SetIcon(defaultIcon, string.Empty);
			
			EVT_TASKBAR_LEFT_DOWN(new wx.EventListener(OnLeftDown));
			EVT_MENU((int) Id.onlineMenu, new wx.EventListener(OnOnlineStatusClicked));
			EVT_MENU((int) Id.awayMenu, new wx.EventListener(OnAwayStatusClicked));
			EVT_MENU((int) Id.invisibleMenu, new wx.EventListener(OnInvisibleStatusClicked));
			EVT_MENU((int) Id.offlineMenu, new wx.EventListener(OnOfflineStatusClicked));
			EVT_MENU((int) Id.settingsMenu, new wx.EventListener(OnSettingsClicked));
			EVT_MENU((int) Id.exitMenu, new wx.EventListener(OnExitClicked));
		}
		
		public void OnLeftDown(object sender, wx.Event evt)
		{
			//TODO: check if there is any incoming chat in 
			// incoming chat queue
			// PARTIALY DONE :p
			if (mainFrame.Dispatch.IncomingChats.Count != 0)
			{
				mainFrame.Dispatch.ActivateIncomingChats();
				return;
			}
			
			if (mainFrame.IsShown)
				mainFrame.Show(false);
			else
				mainFrame.Show(true);
		}
		
		public override wx.Menu CreatePopupMenu()
		{
			if (contexMenu == null)
			{
				contexMenu = new wx.Menu();
					
				wx.MenuItem online = new wx.MenuItem(contexMenu, (int) Id.onlineMenu);
				online.Text = ContactManager.StatusToString(StatusConstans.online);
				online.Bitmap = mainFrame.ImageMgr.StatusBitmaps[StatusConstans.online];
				contexMenu.Append(online);
	
				wx.MenuItem away = new wx.MenuItem(contexMenu, (int) Id.awayMenu);
				away.Text = ContactManager.StatusToString(StatusConstans.away);
				away.Bitmap = mainFrame.ImageMgr.StatusBitmaps[StatusConstans.away];
				contexMenu.Append(away);

				wx.MenuItem invisible = new wx.MenuItem(contexMenu, (int) Id.invisibleMenu);
				invisible.Text = ContactManager.StatusToString(StatusConstans.invisible);
				invisible.Bitmap = mainFrame.ImageMgr.StatusBitmaps[StatusConstans.invisible];
				contexMenu.Append(invisible);
	
				wx.MenuItem offline = new wx.MenuItem(contexMenu, (int) Id.offlineMenu);
				offline.Text = ContactManager.StatusToString(StatusConstans.offline);
				offline.Bitmap = mainFrame.ImageMgr.StatusBitmaps[StatusConstans.offline];
				contexMenu.Append(offline);
				
				contexMenu.AppendSeparator();
				
				wx.MenuItem settings = new wx.MenuItem(contexMenu, (int) Id.settingsMenu);
				settings.Text = Catalog.GetString("Settings");
				settings.Bitmap = mainFrame.ImageMgr[ImageManager.SETTINGS];
				contexMenu.Append(settings);
				
				contexMenu.AppendSeparator();
				
				wx.MenuItem exit = new wx.MenuItem(contexMenu, (int) Id.exitMenu);
				exit.Text = Catalog.GetString("Exit wxGadu");
				exit.Bitmap = mainFrame.ImageMgr[ImageManager.EXIT];
				contexMenu.Append(exit);
			}
			
			return contexMenu;
		}
		
		private void OnOnlineStatusClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Online on tray clicked!"));
#endif
		 	mainFrame.LaunchStatusChangeEvent(StatusConstans.online);
		}

		private void OnAwayStatusClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Away on tray clicked!"));
#endif
			mainFrame.LaunchStatusChangeEvent(StatusConstans.away);
		}

		private void OnOfflineStatusClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Offline on tray clicked!"));
#endif
			mainFrame.LaunchStatusChangeEvent(StatusConstans.offline);
		}

		private void OnInvisibleStatusClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Invisible on tray clicked!"));
#endif
			mainFrame.LaunchStatusChangeEvent(StatusConstans.invisible);
		}
		
		private void OnSettingsClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Settings on tray clicked!"));
#endif
			ConfigurationDialog configDlg = new ConfigurationDialog(mainFrame, -1, Catalog.GetString("Settings"), 
			                                                        wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                                        wx.Dialog.wxDEFAULT_DIALOG_STYLE|wx.Dialog.wxRESIZE_BORDER
			                                                        );
			configDlg.ShowModal();
		}
		
		private void OnExitClicked(object sender, wx.Event evt)
		{
#if LOG
			logger.Trace(Catalog.GetString("Exit on tray clicked!"));
#endif
			mainFrame.PerformRealClose();
			
		}
	}
}
